package com.wit.zhenghao.day11;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.Axis;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart.Data;
import javafx.scene.chart.XYChart.Series;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ScoreChart extends Application {
	private static final String FILE = "D:\\camp2018\\camp2018\\day10\\scores.csv";

	@Override
	public void start(Stage primaryStage) throws Exception {
		VBox box = new VBox(10);
		
		Map<String, List<StudentScore>> courseScores = FileUtils.parseFile(FILE);
		
		for (Entry<String, List<StudentScore>> entry : courseScores.entrySet()) {
			box.getChildren().add(createLineChart(entry.getKey(), entry.getValue()));
		}
		
		Scene scene = new Scene(box);
		primaryStage.setScene(scene);

		primaryStage.show();
	}

	public static void main(String[] args) {
		launch(args);
	}
	
	public static LineChart<String, Number> createLineChart(String title, List<StudentScore> scores) {
		// 创建X轴和Y轴
		Axis<String> xAxis = new CategoryAxis();
		Axis<Number> yAxis = new NumberAxis();
		yAxis.setLabel("分数");
		xAxis.setLabel("姓名");
		
		LineChart<String, Number> lineChart = new LineChart<>(xAxis, yAxis);
		
		Series<String, Number> series = new Series<>();
		// 添加每一个坐标点
		
		for (StudentScore s : scores) {
			series.getData().add(new Data<String, Number>(s.getName(), s.getScore()));
		}
		
		series.setName(title);
		
		lineChart.getData().add(series);
		return lineChart;
	}
}